package com.hgk.wanwan.main.fragment.persentfragment.alarmclock;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import width.hgk.wanwan.R;

public class HaveDone extends Activity implements OnTouchListener,
		OnGestureListener {
	private TextView tvAbout;// ��������
	private Toast toastStart;
	private DatabaseHelper dbHelper; 
	private ListView mListview;
	private SimpleAdapter mAdapter;
	private ArrayList<HashMap<String, Object>> mListItem = null;
	private GestureDetector mGestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tvAbout = (TextView) findViewById(R.id.add);
		tvAbout.setBackgroundResource(R.drawable.selector_about);
		toastStart = new Toast(this);
		tvAbout.setOnClickListener(new OnClickListener() { 

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View view = getLayoutInflater().inflate(R.layout.toast, null);
				toastStart.setGravity(Gravity.BOTTOM, 0, 10);
				toastStart.setDuration(Toast.LENGTH_LONG);
				toastStart.setView(view);
				toastStart.show();
			}
		});

		mGestureDetector = new GestureDetector(this);
		// �����ؼ�ͬʱ�������Ƽ���
		LinearLayout mlayout = (LinearLayout) findViewById(R.id.mainlayout);
		mlayout.setOnTouchListener(this);
		mlayout.setLongClickable(true);

		mListview = (ListView) findViewById(R.id.mlistview);
		mListview.setOnTouchListener(this);
		mListview.setLongClickable(true);
		mListItem = new ArrayList<HashMap<String, Object>>();
		dbHelper = new DatabaseHelper(HaveDone.this, "ideal.sql");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query("user_done", null, null, null, null, null,
				null);
		if (cursor.moveToLast()) {
			HashMap<String, Object> map_last = new HashMap<String, Object>();
			map_last.put("mtext",
					cursor.getString(cursor.getColumnIndex("mtext")));
			map_last.put("mtime",
					cursor.getString(cursor.getColumnIndex("mtime")));
			mListItem.add(map_last);
			while (cursor.moveToPrevious()) {
				String mtext = cursor.getString(cursor.getColumnIndex("mtext"));
				String mtime = cursor.getString(cursor.getColumnIndex("mtime"));
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("mtext", mtext);
				map.put("mtime", mtime);
				mListItem.add(map);
			}
		}

		mAdapter = new SimpleAdapter(getApplicationContext(),
				mListItem,// ���Դ
				R.layout.listviewitem_done, new String[] { "mtext", "mtime" },
				new int[] { R.id.ItemText, R.id.ItemTime });
		mListview.setAdapter(mAdapter);
		db.close();

		mListview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String text = mListItem.get(arg2).get("mtext").toString();
				String time = mListItem.get(arg2).get("mtime").toString();
				Intent intent = new Intent(getApplicationContext(),
						EditTimeActivity.class);
				intent.putExtra("click_key", "click_havedone");
				intent.putExtra("text", text);
				intent.putExtra("time", time);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
		startActivity(intent);
		finish();
		super.onBackPressed();
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 0) {
			Intent intent = new Intent(getApplicationContext(),
					AlarmActivity.class);
			startActivity(intent);
			finish();
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}
}
